package com.android.gtravelling;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class EditActivity extends Activity {

	private DBAdapter mDB;
	private Boolean _isEdit = false;

	private String _name = "";

	TextView txtName;
	TextView txtPhone;
	TextView txtEmail;
	Button btnSave;

	@Override
	public void onCreate(Bundle ds) {
		super.onCreate(ds);
		setContentView(R.layout.new_edit_contact);

		txtName = (TextView) findViewById(R.id.cname_edit);
		txtPhone = (TextView) findViewById(R.id.cphone_edit);
		txtEmail = (TextView) findViewById(R.id.cemail_edit);
		btnSave = (Button) findViewById(R.id.bsave);

		_isEdit = false;
		mDB = new DBAdapter(this);
		mDB.open();

		btnSave.setText("Edit");

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			// Get data via the key
			String value1 = extras.getString(Intent.EXTRA_TEXT);
			this._name = value1;
			if (value1 != null) {
				// TextView txtName = (TextView) findViewById(R.id.cname_edit);
				txtName.setText(value1);
				Cursor emp = mDB.getOneUser(value1);
				
				WebserviceCaller caller=new WebserviceCaller();
				Employee empt=new Employee();
				empt=caller.selectOneuser(value1);

				if (empt != null) {
					/*Employee employ = new Employee();
					if (emp.moveToNext()) {

						employ.setName(emp.getString(emp.getColumnIndex("account")));
						employ.setPhone(emp.getString(emp
								.getColumnIndex("phone")));
						employ.setEmail(emp.getString(emp
								.getColumnIndex("email")));
					}
*/
					
					txtPhone.setText(empt.getPhone());
					txtEmail.setText(empt.getEmail());

					txtName.setEnabled(false);
					txtPhone.setEnabled(false);
					txtEmail.setEnabled(false);
				}
			}
		}

		OnClickListener listen = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (_isEdit == true) {
					if (btnSaveClick() == -1) {
						displayAMessage("Update unsuccessful");
					} else {
						displayAMessage("Update Successful");
					}
					Intent is = new Intent(v.getContext(),
							GTravellingActivity.class);
					// Intent iss=new Intent(Intent.ACTION_EDIT);
					startActivity(is);
				} else {
					txtName.setEnabled(true);
					txtPhone.setEnabled(true);
					txtEmail.setEnabled(true);
					btnSave.setText("Save");
					_isEdit = true;
				}

			}
		};

		btnSave.setOnClickListener(listen);

		Button btnCancel = (Button) findViewById(R.id.bcancel);
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent is = new Intent(v.getContext(), GTravellingActivity.class);
				// Intent iss=new Intent(Intent.ACTION_EDIT);
				startActivity(is);
			}
		});
	}

	public void displayAMessage(String msg) {

		AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Message");
		alertDialog.setMessage(msg);

		alertDialog.show();
	}

	public long btnSaveClick() {

		TextView txtName = (TextView) findViewById(R.id.cname_edit);
		TextView txtPhone = (TextView) findViewById(R.id.cphone_edit);
		TextView txtEmail = (TextView) findViewById(R.id.cemail_edit);

		return mDB.updateUser(txtName.getText().toString(), txtPhone.getText()
				.toString(), txtEmail.getText().toString(), _name);

	}

}
